﻿package rob.entity{
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import rob.parser.ParserOBJ;
	import rob.core.Mesh3d;
	import rob.maths.VectorMaths;
	import rob.data.*;
	import rob.util.StringUtil;
	import rob.core.Ini;
	
	public class Entity{
		private var _mesh:Mesh3d;
		
		public function Entity(s:Sprite):void{
			var _parser:ParserOBJ = ParserOBJ.instance;
			var kx:Number, ky:Number, sx:Number, sy:Number;
			// clone an imported mesh
			var meshName:String = StringUtil.extractMeshString(s.toString());
			_mesh = _parser.getMeshByName(meshName).clone();
			
			// use transform matrix of sprite to determine building size etc
			var mat:Matrix = s.transform.matrix.clone();
			_mesh.updateBBox();
			var bbox:BBox = _mesh.bbox;
		//trace(mat);
			// reset transform matrix
			s.transform.matrix = new Matrix();
			// use s.width to scale world coords -> stage coords
			kx = s.width/bbox.width;
			ky = s.width/bbox.width;
			mat.a *= kx;	mat.c *= kx;
			mat.b *= ky;	mat.d *= ky;
			// must scale 3d height separately, we scale with the width of building
			_mesh.scaleVect(new Vect3d(1,(s.width/bbox.width),1));
			// perform linear transform (this is where we scale as on stage)
			_mesh.transform(mat);
			_mesh.updateNormals();
			// translate
			_mesh.moveTo(new Vert3d(mat.tx ,0,mat.ty ));
			_mesh.putOnFloor();
		}
		
		
		public function get mesh():Mesh3d{return _mesh;}
	}
}